﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using BeautyTech.Core.Data;
using BeautyTech.Core.Domain.Catalogs;

namespace BeautyTech.Services.Catalogs
{
    public class CategoryService : ICategoryService
    {

        #region Fields

        private readonly IRepository<Category> _categories;
        private readonly IRepository<ProductCategory> _productCategoryRepository;
        private readonly IRepository<Product> _productRepository;

        #endregion

        #region Constructor

        public CategoryService(IRepository<Category> categories, IRepository<ProductCategory> productCategoryRepository, IRepository<Product> productRepository)
        {
            this._categories = categories;
            this._productCategoryRepository = productCategoryRepository;
            this._productRepository = productRepository;
        }

        #endregion

        public List<Category> GetAll()
        {
            return _categories.Table.ToList();
        }


        public void Insert(Category model)
        {
            if (model == null)
                throw new ArgumentNullException("category");
            _categories.Insert(model);
        }

        public void Update(Category model)
        {
            if (model == null)
                throw new ArgumentNullException("category");
            _categories.Update(model);

        }

        public void Delete(Category model)
        {
            if (model == null)
                throw new ArgumentNullException("category");
            _categories.Delete(model);
        }

        /// <summary>
        /// Gets a category
        /// </summary>
        /// <param name="categoryId">Category identifier</param>
        /// <returns>Category</returns>
        public virtual Category GetCategoryById(int categoryId)
        {
            if (categoryId == 0)
                return null;

            return _categories.GetById(categoryId);
        }

        /// <summary>
        /// Gets all categories displayed on the home page
        /// </summary>
        /// <returns>Categories</returns>
        public virtual IList<Category> GetAllCategoriesDisplayedOnHomePage()
        {
            var query = from c in _categories.Table
                        orderby c.Order
                        where c.ParentId == 0
                        select c;

            var categories = query.ToList();
            return categories;
        }

        /// <summary>
        /// Gets all categories filtered by parent category identifier
        /// </summary>
        /// <param name="parentCategoryId">Parent category identifier</param>
        /// <returns>Category collection</returns>
        public virtual IList<Category> GetAllCategoriesByParentCategoryId(int parentCategoryId)
        {
            var query = from c in _categories.Table
                        orderby c.Order
                        where c.ParentId == parentCategoryId
                        select c;

            var categories = query.ToList();
            return categories;
        }

        /// <summary>
        /// Gets product category mapping collection
        /// </summary>
        /// <param name="categoryId">Category identifier</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        /// <param name="showHidden">A value indicating whether to show hidden records</param>
        /// <returns>Product a category mapping collection</returns>
        public virtual IList<ProductCategory> GetProductCategoriesByCategoryId(int categoryId)
        {
            if (categoryId == 0)
                return null;

            var query = from pc in _productCategoryRepository.Table
                        join p in _productRepository.Table on pc.ProductId equals p.Id
                        where pc.CategoryId == categoryId 
                        orderby pc.DisplayOrder
                        select pc;

            var productCategories = query.ToList();
            return productCategories;
        }

        /// <summary>
        /// Gets a product category mapping 
        /// </summary>
        /// <param name="productCategoryId">Product category mapping identifier</param>
        /// <returns>Product category mapping</returns>
      /*  public virtual ProductCategory GetProductCategoryById(int productCategoryId)
        {
            if (productCategoryId == 0)
                return null;

            return _productCategory.GetById(productCategoryId);
        }*/

    }
}
